const {
    BASE_URL
} = require('../constants/config.js');
const {
    M_ACCESS_TOKEN,
    M_CLIENT_SESSION_KEY,
} = require('../constants/storage-key.js');

const request = function(method) {
    return function(url, data = {}) {
        return new Promise((resolve, reject) => {
            //拼装token
            data.access_token = wx.getStorageSync(M_ACCESS_TOKEN);

            //请求
            wx.request({
                url: BASE_URL + url,
                data,
                method,
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'systeminfo': JSON.stringify(wx.getStorageSync('systeminfo')),
                    'sharefrominfo': JSON.stringify(wx.getStorageSync('sharefrominfo') || {}),
                    'session_key': wx.getStorageSync(M_CLIENT_SESSION_KEY),
                    'fromrefpathinfo': getLastPageRoute(),
                    'fromrefpathpos_type': getLastLastPagePos()
                },
                success(res) {
                    console.log(res.data);

                    if (res.statusCode === 200 && (res.data.status === 0 || res.data.status === 600)) {
                        resolve(res.data.data);
                    } else {
                        if (res.data.status === 1000) {
                            //1000: 需要登录
                            /* wx.showToast({
                                 title: res.data.msg || '请登录',
                                 icon: 'none'
                             });

                             wx.redirectTo({
                                 // url: INIT_LOGIN,
                             });*/
                        }
                        // console.log('--2222');
                        //console.log(res.data);
                        reject(res.data);
                    }
                },
                fail(err) {
                    console.log(err);
                    let url = '/pages/failed/index';
                    wx.navigateTo({
                        url: url
                    });
                    reject(err);

                }
            });
        });
    };
};


/**
 * 获取上一层页面的route
 */
const getLastPageRoute = function() {
    let pages = getCurrentPages();

    //如果是分享入口进入的子页面，上一页路径直接设置为空
    if (isFromShareEntry()) {
        return '';
    }

    if (pages.length > 1) {
        let lastPage = pages[pages.length - 2];
        return lastPage.route;
    }
    return '';
};

const isFromShareEntry = function() {
    return false;
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    return currentPage.__shareEntryFlag ? true : false;
};

/**
 * 获取上一层页面的标位
 */
const getLastPagePos = function() {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    if (currentPage && currentPage.options && currentPage.options.lastPagePos) {
        return currentPage.options.lastPagePos;
    }
    return '';
};

/**
 * 获取上上层页面的标位
 */
const getLastLastPagePos = function() {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 2];
    if (currentPage && currentPage.options && currentPage.options.lastPagePos) {
        return currentPage.options.lastPagePos;
    }
    return '';
};

module.exports = {
    get: request('GET'),
    post: request('POST')
};